//Copyright (c) Microsoft Corporation.  All rights reserved.

using System;
using System.Collections.Generic;
using System.Text;

namespace Eas.WebCrawler.Common.Collections {
    /// <summary>A static helper class with functions that can be useful when working with arrays.</summary>
    public static class ArrayHelper {
        /// <summary>Combines multiple arrays into one array.</summary>
        /// <typeparam name="_Type">The type contained in the arrays which are to be joined.</typeparam>
        /// <param name="sourceArrays">The source arrays you want to join.</param>
        /// <returns>A new array which joins the source arrays together.</returns>
        /// <remarks>This function maintains the order of the arrays, so that the first source array will contain the lowest index elements in the retruned (combined) awway.
        /// In addition, this function maintains the order of elements in their own arrays.</remarks>
        /// <example><code>string[] a = {"a", "b", "b"};
        /// string[] b = {"c", "z", "g"};
        /// string[] c = {"d", "e","f"}
        /// string[] combined = ArrayHelper.Combine(a, b, c);
        /// foreach(string s in combined){
        /// Console.Write(s);
        /// }</code>
        /// Will write out "abbczgdef" to the console.</example>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1004:GenericMethodsShouldProvideTypeParameter")]
        public static _Type[] Combine<_Type> (params _Type[][] sourceArrays) {
            int totalSize = 0;
            for (int i = 0; i < sourceArrays.Length; i++) {
                totalSize += sourceArrays[i].Length;
            }

            _Type[] returnVale = new _Type[totalSize];
            
            int index=0;
            for (int i = 0; i < sourceArrays.Length; i++) {
                System.Array.Copy(sourceArrays[i], 0, returnVale, index, sourceArrays[i].Length);
                index += sourceArrays[i].Length;
            }

            return returnVale;
        }
    }
}
